define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'layer', 'upload'], function ($, undefined, Backend, Table, Form, Layer, Upload) {
 
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    // // 要关联的拓展信息
                    params.extra = 'distributor,customer';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        {field: 'id', title: 'ID',operate:false},
                        {field: 'nickname', title: '昵称',align:'left'},
                        {field: 'mobile', title: '手机',operate: false},
                        {field: 'gender_name', title: '性别',operate: false},
                        {field: 'point', title: '积分',operate: false},
                        {field: 'product_num', title: '贷款次数',operate: false},
                        {field: 'goods_num', title: '兑换次数',operate: false},
                        {field: 'province', title: '省份',operate: false},
                        {field: 'city', title: '城市',operate: false},

                        {field: 'distributor.nickname', title: '所属分销商'},
                        {field: 'customer.customer_realname', title: '所属客户经理'},

                        {field: 'status', title: '状态', formatter: function (value, row, index) {
                            return (row.status == 1) ? '正常' : '锁定';
                            },operate:false},
                        {field: 'create_time', title: '注册时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        dist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                op:'type=1',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    params.extra = 'customer';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        // {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID',operate:false},
                        {field: 'nickname', title: '昵称',align:'left'},
                        {field: 'mobile', title: '手机',operate: false},
                        {field: 'gender_name', title: '性别',operate: false},
                        {field: 'point', title: '积分',operate: false},
                        {field: 'customer.customer_realname', title: '介绍人',align:'left'},
                        {field: 'product_num', title: '贷款次数',operate: false},
                        {field: 'goods_num', title: '兑换次数',operate: false},
                        {field: 'province', title: '省份',operate: false},
                        {field: 'city', title: '城市',operate: false},
                        {field: 'status', title: '状态', formatter: function (value, row, index) {
                            return (row.status == 1) ? '正常' : '锁定';
                            },operate:false},
                        {field: 'create_time', title: '注册时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        customer: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                op:'type=1',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    // // 要关联的拓展信息
                    params.attrs = 'product_order_total';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        // {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID',operate:false},
                        {field: 'customer_code', title: '编码',operate: false},
                        {field: 'nickname', title: '昵称',align:'left'},
                        {field: 'customer_realname', title: '真实姓名', align:"left"},
                        {field: 'mobile', title: '手机',operate: false},
                        {field: 'gender_name', title: '性别',operate: false},
                        {field: 'province', title: '省份',operate: false},
                        {field: 'city', title: '城市',operate: false},
                        {field: 'point', title: '积分',operate: false},
                        {field: 'goods_num', title: '兑换次数',operate: false},
                        {field: 'product_order_total', title: '业绩总数',operate: false},
                        {field: 'status', title: '状态', formatter: function (value, row, index) {
                            return (row.status == 1) ? '正常' : '锁定';
                            },operate:false},
                        {field: 'create_time', title: '注册时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.customer},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                operate: function (value, row, index) {
                    return '<a class="btn btn-info btn-xs btn-detail"><i class="fa fa-list"></i>设为客户经理</a> ' + Table.api.formatter.operate(value, row, index, $("#table"));
                },
                customer:function (value, row, index) {
                    return '<a class="btn btn-info btn-xs btn-cancel"><i class="fa fa-list"></i>取消客户经理</a> ' + Table.api.formatter.operate(value, row, index, $("#table"));
                }
            },
            events:{
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('/user/admin.customer/create/id/' + row['id'], __('Detail'));
                    },
                    'click .btn-cancel' : function (e, value, row, index) {
                        e.stopPropagation();
                        // Backend.api.url();

                        if(confirm("您确认要取消该客户经理资格？")) {
                            Fast.api.ajax({
                                url : '/user/admin.customer/cancel',
                                data : {
                                    id : row['id']
                                }
                            });
                        }
                    }
                }, Table.api.events.operate)
            }
        }
    }
    return Controller;
});